package com.jingdianjichi.auth.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.convert.AuthPermissionDTOConverter;
import com.jingdianjichi.auth.domain.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import com.jingdianjichi.auth.dto.AuthPermissionDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/permission/")
public class PermissionController {

    @Resource
    AuthPermissionDomainService authPermissionDomainService;

    @RequestMapping("add")
    public Result<Boolean> register(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("add subjectdto category", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(authPermissionDTO.getName()));
            Preconditions.checkArgument(!StringUtils.isEmpty(authPermissionDTO.getPermissionKey()));
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBoAuthPermission(authPermissionDTO);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            return Result.fail("添加权限失败");
        }
    }

    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("add subjectdto category", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId());
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBoAuthPermission(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        } catch (Exception e) {
            return Result.fail("更新权限失败");
        }
    }
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("add subjectdto category", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId());
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBoAuthPermission(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        } catch (Exception e) {
            return Result.fail("删除权限失败");
        }
    }
    @RequestMapping("getPermission")
    public Result<AuthPermissionDTO> getPermission(@RequestParam String userName) {
        try {
            log.info("username", JSON.toJSONString(userName));
            Preconditions.checkArgument(!StringUtils.isEmpty(userName), "用户名不能为空");
            List<String> permission = authPermissionDomainService.getPermission(userName);
            return Result.ok(permission);
        } catch (Exception e) {
            return Result.fail("查询权限失败");
        }
    }
}
